package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Member;
import com.itheima.pojo.Memberf;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    //根据手机号查询会员信息
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public Memberf findByTelephonef(String telephone) {
        return memberDao.findByTelephonef(telephone);
    }

    //新增会员
    public void add(Member member) {
        //如果用户设置了密码，需要对密码进行md5加密
        String password = member.getPassword();
        if(password != null){
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    @Override
    public void addAll(Memberf memberf) {
        memberf.setRegTime(new Date());
        memberDao.addAllMember(memberf);
        memberDao.addInfo(memberf);
        memberDao.addDis(memberf);

    }

    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCounts = new ArrayList<>();
        if(months != null && months.size() > 0){
            for (String month : months) {//2019.01
                String endTime = month + ".31";
                Integer memberCount = memberDao.findMemberCountBeforeDate(endTime);
                memberCounts.add(memberCount);
            }
        }
        return memberCounts;
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Memberf> page = memberDao.selectByConditionf(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        memberDao.deleteById(id);
        memberDao.deleteById_info(id);
        memberDao.deleteById_dis(id);
    }

    @Override
    public Memberf findById(Integer id) {

        long countByInfo = memberDao.findByInfoCount(id);
        long countByDis = memberDao.findByDisCount(id);

        if (countByDis>0 && countByInfo>0){
            return memberDao.findByIdf(id);
        } else {
            if (countByDis<1 && countByInfo>0){
                return memberDao.findByInfo(id);
            } else {
                if (countByInfo<1 && countByDis>0){
                    return memberDao.findByDis(id);
                } else {
                    return memberDao.findByOnly(id);
                }
            }
        }
    }

    @Override
    public void edit(Memberf memberf) {
        memberDao.editByMember(memberf);
        long byInfoCount = memberDao.findByInfoCount(memberf.getId());
        long byDisCount = memberDao.findByDisCount(memberf.getId());
        if (byInfoCount>0){
            memberDao.editByInfo(memberf);
        } else {
            memberDao.addInfo(memberf);
        }
        if (byDisCount>0){
            memberDao.editByDis(memberf);
        } else {
            memberDao.addDis(memberf);
        }
    }

    @Override
    public List<Integer> findMemberCountByMouth(List<String> result) {
        List<Integer> counts = new ArrayList<>();
        for (String dataString : result) {
            String month = dataString.replace(".", "-");
            Integer count = memberDao.findMemberCountByMouth(month);
            counts.add(count);
        }
        return counts;
    }
}
